<?php 

class GoodsModel extends Model{
	//格式化表
	public $table='goods';
	//自动完成函数
	public $auto=array(
			array('addtime','strtotime','function',2,3),
			array('index_image','_index_image','method',2,3),
			array('list_image','_list_image','method',2,3)
		);
	//自动处理图像函数，$v表示POST里的值
	public function _index_image($v){
		$upload=new Upload;
		$file=$upload->upload('index_image');
		return empty($file) ? $v : $file[0]['path'];
	}
	public function _list_image($v){
		$upload=new Upload;
		$file=$upload->upload('list_image'); 
		return empty($file) ? $v : $file[0]['path'];
	}
	//添加商品函数
	public function addGoods(){
		if ($this->create()) {
			if ($gid=$this->add()) {
				//设置详情页图片
				$this->thumbImage($gid);
				$this->setGoodsSpec($_POST['cid'], $gid);
				//添加商品的基本属性
				// p($_POST);die;
				$this -> setGoodsAttr($_POST['cid'], $gid);
			return true;
			}	
		}
	}

	//修改商品函数
	public function editGoods(){
		// p($_POST);
		//完成自动验证
		if ($this->create()) {
			//完成自动验证之后，$_POST里面的值经过处理后回存到$this->data中，便于完成之后的修改操作
			// p($this->data);die;
			$this->save($this->data);
			M('stock')->del('gid='.$_POST['gid']);
			M('goods_attr_list')->del('gid='.$_POST['gid']);
			M('stock_attr')->del('gid='.$_POST['gid']);
			$this->thumbImage($_POST['gid']);
			$this->setGoodsSpec($_POST['cid'],$_POST['gid']);
			$this->setGoodsAttr($_POST['cid'],$_POST['gid']);
			return true;
		}
	}
	//处理图像方法
	private function thumbImage($gid){
		$upload =new Upload();
		$images =$upload->upload('image');
		$imageObj = new Image();
				$data=array();
				foreach($images as $img){
					//小图
					$tmp['small']=$imageObj->thumb($img['path'],$img['dir'].$img['filename'].'_small.'.$img['ext'],50,50);
					//中图
					$tmp['medium']=$imageObj->thumb($img['path'],$img['dir'].$img['filename'].'_medium.'.$img['ext'],350,350);
					//大图
					$tmp['big']=$img['path'];
					$tmp['gid']=$gid;
					M('goods_image')->add($tmp);
				}
	}
	//获得并格式化商品属性值
	private function getAttrData() {
		if (empty($_POST['attr']))
			return;
		$attr = $_POST['attr'];
		$data = array();
		$id = 0;
		foreach ($attr as $name => $value) {
			$info = explode('-', $name);
			//$info[0] 类型如radio,checkbox
			switch($info[0]) {
				case 'radio' :
				case 'checkbox' :
				case 'select' :
					$t = explode('-', $value);
					$data[$id]['avid'] = $t[0];
					$data[$id]['attr_name'] = $t[1];
					break;
				case 'text' :
					$data[$id]['avid'] = $info[2];
					$data[$id]['attr_name'] = $value;
					break;
			}
			$id++;
		}
		return $data;
	}
	//添加商品基本属性
	public function setGoodsAttr($cid, $gid) {
		$data = $this -> getAttrData();
		if (empty($data))
			return;
		$db = M('goods_attr_list');
		$db -> where("gid=$gid") -> del();
		foreach ($data as $d) {
			$d['cid'] = $cid;
			$d['gid'] = $gid;
			$db -> add($d);
		}
		return true;
	}
	//添加商品规格属性
	public function setGoodsSpec($cid,$gid){
		$spec=$_POST['spec'];
		$stockdata=array();
		$specdata=array();
		$attrdata=array();
		foreach ($spec['price'] as $id => $value) {
			if (!empty($value) || !empty($spec['stock'][$id]) || !empty($spec['sn'][$id])) {
				$stockdata['price']=$value;
				$stockdata['stock']=$spec['stock'][$id];
				$stockdata['sn']=$spec['sn'][$id];
				$stockdata['gid']=$gid;
				if($sid=M('stock')->add($stockdata)){
					foreach ($spec['select'] as $k => $v) {
						$avid=explode('-',$v[$id]);
						$specdata['cid']=$cid;
						$specdata['gid']=$gid;
						$specdata['sid']=$sid;
						$specdata['avid']=$avid[0];
						M('stock_attr')->add($specdata);
						$_POST['attr']['select-'.$avid[0]]=$v[$id];
					}
				}
			}
		}

		// p($attrdata);die;
	}

}
?>